package com.betfair.wrapper.types;

import demo.util.InflatedMarketPrices;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: markwilliams
 * Date: 10/19/13
 * Time: 3:32 PM
 * To change this template use File | Settings | File Templates.
 */
@Entity
@Table(name = "MarketPrice", uniqueConstraints = {
        @UniqueConstraint(columnNames = "iMarketPriceId")})
public class MarketPrice {
    int _id;
    InflatedMarketPrices _prices = new InflatedMarketPrices();
    MarketWrap _market = null;
    Map<Integer, Runner> runners = new HashMap<Integer, Runner>();

    public MarketPrice() {
    }

    public MarketPrice(InflatedMarketPrices prices) {
        this.setPrices(prices);
    }

    @Id
    @GeneratedValue(generator = "increment")
    @GenericGenerator(name = "increment", strategy = "increment")
    @Column(name = "iMarketPriceId")
    public int getId() {
        return _id;
    }

    public void setId(int id) {
        this._id = id;
    }

    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "iMarketId", nullable = false)
    public MarketWrap getMarket() {
        return _market;
    }

    public void setMarket(MarketWrap market) {
        this._market = market;
        for (InflatedMarketPrices.InflatedRunner ir : _prices.getRunners()) {
            Runner r = runners.get(ir.getSelectionId());
            com.betfair.www.publicapi.types.exchange.v5.Runner marketRunner = this.getMarket().getRunnerMap().get(ir.getSelectionId());
            if (marketRunner != null) {
                r.setMarketRunner(marketRunner);
            }
        }
    }

    public void setPrices(InflatedMarketPrices prices) {
        this._prices = prices;

        for (InflatedMarketPrices.InflatedRunner ir : _prices.getRunners()) {
            Runner r = runners.get(ir.getSelectionId());
            if (r != null) {
                r.setRunner(ir);
            } else {
                r = new Runner(ir);
                r.setMarketPrice(this);
                runners.put(r.getSelectionId(), r);
            }
        }
    }
//
//    private void setupRunners() {
//        runners = new HashMap<Integer, Runner>();
//        for (InflatedMarketPrices.InflatedRunner ir : _prices.getRunners()) {
//            Runner r = new Runner(ir);
//            r.setMarketPrice(this);
//
//            runners.put(r.getSelectionId(), r);
//        }
//    }

    public int getMarketId() {
        return _prices.getMarketId();
    }

    private void setMarketId(int param) {
    }

    public boolean isBSPMarket() {
        return _prices.isBSPMarket();
    }

    private void setBSPMarket(boolean param) {
    }

    public long getRefreshTime() {
        return _prices.getRefreshTime();
    }

    private void setRefreshTime(long param) {
    }

    public int getNumberofWinners() {
        return _prices.getNumberofWinners();
    }

    private void setNumberofWinners(int param) {
    }

    public String getCurrency() {
        return _prices.getCurrency();
    }

    private void setCurrency(String param) {
    }

    public int getInPlayDelay() {
        return _prices.getInPlayDelay();
    }

    private void setInPlayDelay(int param) {
    }

    public String getMarketStatus() {
        return _prices.getMarketStatus();
    }

    private void setMarketStatus(String param) {
    }

    public String getMarketBaseRate() {
        return _prices.getMarketBaseRate();
    }

    private void setMarketBaseRate(String param) {
    }

    public boolean isDiscountAllowed() {
        return _prices.isDiscountAllowed();
    }

    private void setDiscountAllowed(boolean param) {
    }

    public String getMarketInformation() {
        return _prices.getMarketInformation();
    }

    private void setMarketInformation(String param) {
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "marketPrice")
    @Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE})
    public Set<Runner> getRunners() {
        return new HashSet<Runner>(runners.values());
    }

    private void setRunners(Set<Runner> param) {
    }

    @Transient
    public List<InflatedMarketPrices.InflatedRemovedRunner> getRemovedRunners() {
        return _prices.getRemovedRunners();
    }
}
